home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  11.2 KB  |  399 lines

  1. /* Work with executable files, for GDB. 
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "target.h"
  26. #include "gdbcmd.h"
  27.  
  28. #ifdef USG
  29. #include <sys/types.h>
  30. #endif
  31.  
  32. #include <sys/param.h>
  33. #include <fcntl.h>
  34. #include <string.h>
  35.  
  36. #include "gdbcore.h"
  37.  
  38. #ifdef STILL_NEEDED_FOR_DECSTATION
  39. #include <sys/dir.h>        /* For DECstations */
  40. #include <sys/user.h>        /* After a.out.h  */
  41. #include <sys/file.h>
  42. #endif
  43.  
  44. #include <ctype.h>
  45. #include <sys/stat.h>
  46.  
  47. extern char *getenv();
  48. extern void child_create_inferior (), child_attach ();
  49. extern void symbol_file_command ();
  50.  
  51. /* The Binary File Descriptor handle for the executable file.  */
  52.  
  53. bfd *exec_bfd = NULL;
  54.  
  55. /* Forward decl */
  56.  
  57. extern struct target_ops exec_ops;
  58.  
  59. /* ARGSUSED */
  60. void
  61. exec_close (quitting)
  62.      int quitting;
  63. {
  64.   if (exec_bfd) {
  65.     bfd_close (exec_bfd);
  66.     exec_bfd = NULL;
  67.   }
  68.   if (exec_ops.sections) {
  69.     free (exec_ops.sections);
  70.     exec_ops.sections = NULL;
  71.     exec_ops.sections_end = NULL;
  72.   }
  73. }
  74.  
  75. void
  76. exec_file_command (filename, from_tty)
  77.      char *filename;
  78.      int from_tty;
  79. {
  80.   target_preopen (from_tty);
  81.  
  82.   /* Remove any previous exec file.  */
  83.   unpush_target (&exec_ops);
  84.  
  85.   /* Now open and digest the file the user requested, if any.  */
  86.  
  87.   if (filename)
  88.     {
  89.       char *scratch_pathname;
  90.       int scratch_chan;
  91.       
  92.       filename = tilde_expand (filename);
  93.       make_cleanup (free, filename);
  94.       
  95. /* FIXME, if writeable is set, open for read/write. */
  96.       scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  97.                 &scratch_pathname);
  98.       if (scratch_chan < 0)
  99.     perror_with_name (filename);
  100.  
  101.       exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
  102.       if (!exec_bfd)
  103.     error ("Could not open `%s' as an executable file: %s",
  104.            scratch_pathname, bfd_errmsg (bfd_error));
  105.       if (!bfd_check_format (exec_bfd, bfd_object))
  106.     error ("\"%s\": not in executable format: %s.",
  107.            scratch_pathname, bfd_errmsg (bfd_error));
  108.  
  109. #if FIXME
  110. /* This code needs to be incorporated into BFD */
  111. #ifdef COFF_ENCAPSULATE
  112.     /* If we have a coff header, it can give us better values for
  113.        text_start and exec_data_start.  This is particularly useful
  114.        for remote debugging of embedded systems.  */
  115.     if (N_FLAGS(exec_aouthdr) & N_FLAGS_COFF_ENCAPSULATE)
  116.     {
  117.         struct coffheader ch;
  118.         int val;
  119.         val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1);
  120.         if (val == -1)
  121.             perror_with_name (filename);
  122.         val = myread (execchan, &ch, sizeof (ch));
  123.         if (val < 0)
  124.             perror_with_name (filename);
  125.         text_start = ch.text_start;
  126.         exec_data_start = ch.data_start;
  127.     } else
  128. #endif
  129.            {
  130.         text_start =
  131.           IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
  132.         exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  133.           ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  134.     }
  135. #endif FIXME
  136.  
  137.       if (build_section_table (exec_bfd, &exec_ops.sections,
  138.                 &exec_ops.sections_end))
  139.     error ("Can't find the file sections in `%s': %s", 
  140.         exec_bfd->filename, bfd_errmsg (bfd_error));
  141.  
  142.       validate_files ();
  143.  
  144.       push_target (&exec_ops);
  145.  
  146.       /* Tell display code (if any) about the changed file name.  */
  147.       if (exec_file_display_hook)
  148.     (*exec_file_display_hook) (filename);
  149.     }
  150.   else if (from_tty)
  151.     printf ("No exec file now.\n");
  152. }
  153.  
  154. /* Set both the exec file and the symbol file, in one command.  
  155.    What a novelty.  Why did GDB go through four major releases before this
  156.    command was added?  */
  157.  
  158. void
  159. file_command (arg, from_tty)
  160.      char *arg;
  161.      int from_tty;
  162. {
  163.   /* FIXME, if we lose on reading the symbol file, we should revert
  164.      the exec file, but that's rough.  */
  165.   exec_file_command (arg, from_tty);
  166.   symbol_file_command (arg, from_tty);
  167. }
  168.  
  169.  
  170. /* Locate all mappable sections of a BFD file. 
  171.    table_pp_char is a char * to get it through bfd_map_over_sections;
  172.    we cast it back to its proper type.  */
  173.  
  174. void
  175. add_to_section_table (abfd, asect, table_pp_char)
  176.      bfd *abfd;
  177.      sec_ptr asect;
  178.      char *table_pp_char;
  179. {
  180.   struct section_table **table_pp = (struct section_table **)table_pp_char;
  181.   flagword aflag;
  182.  
  183.   aflag = bfd_get_section_flags (abfd, asect);
  184.   /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
  185.   if (!(aflag & SEC_LOAD))
  186.     return;
  187.   (*table_pp)->bfd = abfd;
  188.   (*table_pp)->sec_ptr = asect;
  189.   (*table_pp)->addr = bfd_section_vma (abfd, asect);
  190.   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
  191.   (*table_pp)++;
  192. }
  193.  
  194. int
  195. build_section_table (some_bfd, start, end)
  196.      bfd *some_bfd;
  197.      struct section_table **start, **end;
  198. {
  199.   unsigned count;
  200.  
  201.   count = bfd_count_sections (some_bfd);
  202.   if (count == 0)
  203.     abort();    /* return 1? */
  204.   if (*start)
  205.     free (*start);
  206.   *start = (struct section_table *) xmalloc (count * sizeof (**start));
  207.   *end = *start;
  208.   bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
  209.   if (*end > *start + count)
  210.     abort();
  211.   /* We could realloc the table, but it probably loses for most files.  */
  212.   return 0;
  213. }
  214.  
  215. /* Read or write the exec file.
  216.  
  217.    Args are address within a BFD file, address within gdb address-space,
  218.    length, and a flag indicating whether to read or write.
  219.  
  220.    Result is a length:
  221.  
  222.     0:    We cannot handle this address and length.
  223.     > 0:  We have handled N bytes starting at this address.
  224.           (If N == length, we did it all.)  We might be able
  225.           to handle more bytes beyond this length, but no
  226.           promises.
  227.     < 0:  We cannot handle this address, but if somebody
  228.           else handles (-N) bytes, we can start from there.
  229.  
  230.     The same routine is used to handle both core and exec files;
  231.     we just tail-call it with more arguments to select between them.  */
  232.  
  233. int
  234. xfer_memory (memaddr, myaddr, len, write, target)
  235.      CORE_ADDR memaddr;
  236.      char *myaddr;
  237.      int len;
  238.      int write;
  239.      struct target_ops *target;
  240. {
  241.   boolean res;
  242.   struct section_table *p;
  243.   CORE_ADDR nextsectaddr, memend;
  244.   boolean (*xfer_fn) ();
  245.  
  246.   if (len <= 0)
  247.     abort();
  248.  
  249.   memend = memaddr + len;
  250.   xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
  251.   nextsectaddr = memend;
  252.  
  253.   for (p = target->sections; p < target->sections_end; p++)
  254.     {
  255.       if (p->addr <= memaddr)
  256.     if (p->endaddr >= memend)
  257.       {
  258.         /* Entire transfer is within this section.  */
  259.         res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
  260.         return (res != false)? len: 0;
  261.       }
  262.     else if (p->endaddr <= memaddr)
  263.       {
  264.         /* This section ends before the transfer starts.  */
  265.         continue;
  266.       }
  267.     else 
  268.       {
  269.         /* This section overlaps the transfer.  Just do half.  */
  270.         len = p->endaddr - memaddr;
  271.         res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
  272.         return (res != false)? len: 0;
  273.       }
  274.       else if (p->addr < nextsectaddr)
  275.     nextsectaddr = p->addr;
  276.     }
  277.  
  278.   if (nextsectaddr >= memend)
  279.     return 0;                /* We can't help */
  280.   else
  281.     return - (nextsectaddr - memaddr);    /* Next boundary where we can help */
  282. }
  283.  
  284. #ifdef FIXME
  285. #ifdef REG_STACK_SEGMENT
  286. /* MOVE TO BFD... */
  287.     /* Pyramids and AM29000s have an extra segment in the virtual address space
  288.        for the (control) stack of register-window frames.  The AM29000 folk
  289.        call it the "register stack" rather than the "memory stack".  */
  290.     else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
  291.       {
  292.     i = min (len, reg_stack_end - memaddr);
  293.     fileptr = memaddr - reg_stack_start + reg_stack_offset;
  294.     wanna_xfer = coredata;
  295.       }
  296. #endif                /* REG_STACK_SEGMENT */
  297. #endif FIXME
  298.  
  299. static void
  300. exec_files_info ()
  301. {
  302.   struct section_table *p;
  303.  
  304.   printf ("\tExecutable file `%s'.\n", bfd_get_filename(exec_bfd));
  305.  
  306.   for (p = exec_ops.sections; p < exec_ops.sections_end; p++)
  307.     printf("\texecutable from 0x%08x to 0x%08x is %s\n",
  308.     p->addr, p->endaddr,
  309.     bfd_section_name (exec_bfd, p->sec_ptr));
  310. }
  311.  
  312. static void
  313. set_section_command (args, from_tty)
  314.      char *args;
  315.      int from_tty;
  316. {
  317.   struct section_table *p;
  318.   char *secname;
  319.   unsigned seclen;
  320.   unsigned long secaddr;
  321.   char secprint[100];
  322.   long offset;
  323.  
  324.   if (args == 0)
  325.     error ("Must specify section name and its virtual address");
  326.  
  327.   /* Parse out section name */
  328.   for (secname = args; !isspace(*args); args++) ;
  329.   seclen = args - secname;
  330.  
  331.   /* Parse out new virtual address */
  332.   secaddr = parse_and_eval_address (args);
  333.  
  334.   for (p = exec_ops.sections; p < exec_ops.sections_end; p++) {
  335.     if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
  336.     && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
  337.       offset = secaddr - p->addr;
  338.       p->addr += offset;
  339.       p->endaddr += offset;
  340.       exec_files_info();
  341.       return;
  342.     }
  343.   } 
  344.   if (seclen >= sizeof (secprint))
  345.     seclen = sizeof (secprint) - 1;
  346.   strncpy (secprint, secname, seclen);
  347.   secprint[seclen] = '\0';
  348.   error ("Section %s not found", secprint);
  349. }
  350.  
  351. struct target_ops exec_ops = {
  352.     "exec", "Local exec file",
  353.     "Use an executable file as a target.\n\
  354. Specify the filename of the executable file.",
  355.     exec_file_command, exec_close, /* open, close */
  356.     child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
  357.     0, 0, /* fetch_registers, store_registers, */
  358.     0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
  359.     xfer_memory, exec_files_info,
  360.     0, 0, /* insert_breakpoint, remove_breakpoint, */
  361.     0, 0, 0, 0, 0, /* terminal stuff */
  362.     0, 0, /* kill, load */
  363.     0, 0, /* call fn, lookup sym */
  364.     child_create_inferior,
  365.     0, /* mourn_inferior */
  366.     file_stratum, 0, /* next */
  367.     0, 1, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  368.     0, 0,            /* section pointers */
  369.     OPS_MAGIC,        /* Always the last thing */
  370. };
  371.  
  372. void
  373. _initialize_exec()
  374. {
  375.  
  376.   add_com ("file", class_files, file_command,
  377.        "Use FILE as program to be debugged.\n\
  378. It is read for its symbols, for getting the contents of pure memory,\n\
  379. and it is the program executed when you use the `run' command.\n\
  380. If FILE cannot be found as specified, your execution directory path\n\
  381. ($PATH) is searched for a command of that name.\n\
  382. No arg means to have no executable file and no symbols.");
  383.  
  384.   add_com ("exec-file", class_files, exec_file_command,
  385.        "Use FILE as program for getting contents of pure memory.\n\
  386. If FILE cannot be found as specified, your execution directory path\n\
  387. is searched for a command of that name.\n\
  388. No arg means have no executable file.");
  389.  
  390.   add_com ("section", class_files, set_section_command,
  391.    "Change the base address of section SECTION of the exec file to ADDR.\n\
  392. This can be used if the exec file does not contain section addresses,\n\
  393. (such as in the a.out format), or when the addresses specified in the\n\
  394. file itself are wrong.  Each section must be changed separately.  The\n\
  395. ``info files'' command lists all the sections and their addresses.");
  396.  
  397.   add_target (&exec_ops);
  398. }
  399.